package cn.net.vote.data.async;

import java.util.concurrent.CompletableFuture;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson.JSON;

import cn.net.vote.data.domain.PageData;
import cn.net.vote.util.ConfigUtil;

@Service
public class NationalData {
	protected Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	RestTemplate rest;


	@Async
	public CompletableFuture<Integer> calc(int n) {
		try {
			Thread.sleep(n * 1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		return CompletableFuture.completedFuture(n);
	}

	
	public Object getDataByDataService(String person_idcarid, String police_idcard, String police_unitCode, String police_name,String token){
		logger.info("进行人员状态查询  : ");
		Object object = null;
		String result = "";
		String url =  ConfigUtil.getString("custom.local-data-service.url");
		logger.info(url);
		PageData pd = new PageData();
		pd.put("target","person" );
		pd.put("name", "900902".equals(ConfigUtil.getString("custom.developmode"))?(""+System.currentTimeMillis()):"");
		pd.put("sfzh",person_idcarid );
		pd.put("type",ConfigUtil.getString("custom.stay.method") );
		pd.put("jybmbh", police_unitCode);
		pd.put("jysfzh",police_idcard );
		pd.put("jyxm", police_name);
		HttpHeaders headers = new HttpHeaders();
		MediaType mediaType = MediaType.parseMediaType("application/json; charset=UTF-8");
		headers.setContentType(mediaType);
		headers.add("Accept", MediaType.APPLICATION_JSON.toString());
		headers.add("token", token);
		HttpEntity<String> formEntity = new HttpEntity<String>(JSON.toJSON(pd).toString(), headers);
		 ResponseEntity<String> responseEntity = rest.exchange(url,
	                HttpMethod.POST, formEntity, String.class);
	        HttpStatus status = responseEntity.getStatusCode();
		//result = rest.postForObject(url, formEntity, String.class);
	      
	        if(status.value()==200){
	        	  result = responseEntity.getBody();
	        	  com.alibaba.fastjson.JSONObject  obj = JSON.parseObject(result);
	        	  object = obj.get("result");
	        }
		logger.info("result  : "+result);
		logger.info("status  : "+status.value());
		return object;
	}
	
}
